#include "class_position.h"

class_position::class_position()
{
memset(&p22, 0, sizeof(CThostFtdcInvestorPositionField));
memset(&p23, 0, sizeof(CThostFtdcInvestorPositionField));
memset(&p12, 0, sizeof(CThostFtdcInvestorPositionField));
memset(&p13, 0, sizeof(CThostFtdcInvestorPositionField));
value = 0;
}

class_position::~class_position()
{

}

// 插入持仓查询响应内容
int class_position::insert_position(CThostFtdcInvestorPositionField data)
{
lock.begin();//进入临界区
if (data.PositionDate == '1')
{
if (data.PosiDirection == '2')
{
memcpy(&p12, &data, sizeof(CThostFtdcInvestorPositionField));
}
else {
memcpy(&p13, &data, sizeof(CThostFtdcInvestorPositionField));
}
} 
else {
if (data.PosiDirection == '2')
{
memcpy(&p22, &data, sizeof(CThostFtdcInvestorPositionField));
}
else {
memcpy(&p23, &data, sizeof(CThostFtdcInvestorPositionField));
}
}
lock.end();//离开临界区
return 0;
}

// 插入报单通知内容
int class_position::insert_order(CThostFtdcOrderField data)
{

	std::stringstream key;
key << data.OrderRef << "_"<< data.SessionID <<"_"<< data.FrontID;

//初次收到报单通知
if (order_map.count(key.str()) == 0)
{
if (data.Direction == '0')
{
if (strcmp(data.CombOffsetFlag, "0") == 0) //开金仓多
{
lock.begin();//加锁
p12.LongFrozen += data.VolumeTotalOriginal;
lock.end();//解锁
}

if (strcmp(data.CombOffsetFlag, "1") == 0) //瓶仓空
{
lock.begin();//加锁
p13.LongFrozen += data.VolumeTotalOriginal;
lock.end();//解锁
}

if (strcmp(data.CombOffsetFlag, "3") == 0) //瓶金仓空
{
lock.begin();//加锁
p13.LongFrozen += data.VolumeTotalOriginal;
lock.end();//解锁
}

if (strcmp(data.CombOffsetFlag, "4") == 0) //瓶昨仓空
{
lock.begin();//加锁
p23.LongFrozen += data.VolumeTotalOriginal;
lock.end();//解锁
}

}
else  //下面是卖出的
{
if (strcmp(data.CombOffsetFlag, "0") == 0) //开仓空
{
lock.begin();//加锁
p13.ShortFrozen += data.VolumeTotalOriginal;
lock.end();//解锁
}

if (strcmp(data.CombOffsetFlag, "1") == 0) //平仓多
{
lock.begin();//加锁
p12.ShortFrozen += data.VolumeTotalOriginal;
lock.end();//解锁
}

if (strcmp(data.CombOffsetFlag, "3") == 0) //平仓多
{
lock.begin();//加锁
p12.ShortFrozen += data.VolumeTotalOriginal;
lock.end();//解锁
}

if (strcmp(data.CombOffsetFlag, "4") == 0) //平昨仓多
{
lock.begin();//加锁
p22.ShortFrozen += data.VolumeTotalOriginal;
lock.end();//解锁
}
}
}

//插入
order_map[key.str()] = data;

if (data.OrderStatus == '5') //如果是撤单的情况
{
if (data.Direction == '0') //如果是开仓
{
if (strcmp(data.CombOffsetFlag, "0") == 0)//开金仓多
{
lock.begin();//加锁
p12.LongFrozen -= data.VolumeTotal;
lock.end();//解锁
}
if (strcmp(data.CombOffsetFlag, "1") == 0) //瓶仓多
{
lock.begin();//加锁
p13.LongFrozen -= data.VolumeTotal;
lock.end();//解锁
}
if (strcmp(data.CombOffsetFlag, "3") == 0) //瓶今仓多
{
lock.begin();//加锁
p13.LongFrozen -= data.VolumeTotal;
lock.end();//解锁
}
if (strcmp(data.CombOffsetFlag, "4") == 0)//瓶昨仓多
{
lock.begin();//加锁
p23.LongFrozen -= data.VolumeTotal;
lock.end();//解锁
}
}
else  //下面是卖出的
{
if (strcmp(data.CombOffsetFlag, "0") == 0) //开金仓空
{
lock.begin();//加锁
p13.ShortFrozen -= data.VolumeTotal;
lock.end();//解锁
}
if (strcmp(data.CombOffsetFlag, "1") == 0) //瓶仓空
{
lock.begin();//加锁
p12.ShortFrozen -= data.VolumeTotal;
lock.end();//解锁
}
if (strcmp(data.CombOffsetFlag, "3") == 0)//瓶今仓空
{
lock.begin();//加锁
p12.ShortFrozen -= data.VolumeTotal;
lock.end();//解锁
}
if (strcmp(data.CombOffsetFlag, "4") == 0) //瓶昨仓空
{
lock.begin();//加锁
p22.ShortFrozen -= data.VolumeTotal;
lock.end();//解锁
}
}
}

return 0;
}

 //插入成交通知
int class_position::insert_trade(CThostFtdcTradeField data)
{

if (data.Direction == '0')//多
{
switch (data.OffsetFlag)//开平标志
{
case '0': //开多
lock.begin();//加锁
p12.Position += data.Volume;
p12.LongFrozen -= data.Volume;
	value -= (data.Volume * data.Price);
	lock.end();//解锁
break;

case '1': //平仓
lock.begin();//加锁
p13.Position -= data.Volume;
p13.LongFrozen -= data.Volume;
value -= (data.Volume * data.Price);
lock.end();//解锁
break;

case '3': //平今仓
lock.begin();//加锁
p13.Position -= data.Volume;
p13.LongFrozen -= data.Volume;
value -= (data.Volume * data.Price);
lock.end();//解锁
break;

case '4': //瓶昨仓
lock.begin();//加锁
p23.Position -= data.Volume;
p23.LongFrozen -= data.Volume;
value -= (data.Volume * data.Price);
lock.end();//解锁
break;
}

}
else //空
{
switch (data.OffsetFlag)
{
case '0': //开仓
lock.begin();//加锁
p13.Position += data.Volume;
p13.ShortFrozen -= data.Volume;
value += (data.Volume * data.Price);
lock.end();//解锁
break;

case '1': // 卖出平仓
lock.begin();//加锁
p12.Position -= data.Volume;
p12.ShortFrozen -= data.Volume;
value += (data.Volume * data.Price);
lock.end();//解锁
break;

case '3': //卖出瓶金仓
lock.begin();//加锁
p12.Position -= data.Volume;
p12.ShortFrozen -= data.Volume;
value += (data.Volume * data.Price);
lock.end();//解锁
break;

case '4': //
lock.begin();//加锁
p22.Position -= data.Volume;
p22.ShortFrozen -= data.Volume;
value += (data.Volume * data.Price);
lock.end();//解锁
break;
}

}

trade_list.push_back(data);
return 0;
}


